package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.*;
import Function.FCMutilPlayCache;
import Function.FCMutilPlayPropulsionCache;
import VWorldGameStudio.events.ShopEvent;

import java.util.Vector;
import java.util.function.BiConsumer;

import static FCRendering.FCRMaster.*;

/**
 * 战车出租
 */
public class FunctionTanZuCheShop extends AShopFuntion {
    private Vector<String> itemNames = new Vector<>();
    private Vector<Integer> as = new Vector<>();
    private int showFristIndex = 0;

    private int endI = 0;
    private int willBuyPerPrice = 0;
    private String willBuyItemName = "null";

    private int page = 0;//0是首页，1是借车， 2是还车 3是购买下来

    private void setEndIndex(int num) {
        endI = num;
    }

    public FunctionTanZuCheShop() {
        buildShopWindow();
    }


    public void buildShopWindow() {
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        if (page == 0) {
            FCRValues.leftWindowUI[0].leftText = "租车";
            FCRValues.leftWindowUI[1].leftText = "还车";
            FCRValues.leftWindowUI[2].leftText = "购买战车";
        } else if (page == 1 || page == 3) {
            if (page == 3) {
                FCRValues.leftWindowUI[0].leftText = "购买战车";
                FCRValues.rightWindowUI[5].rightText = "购买";
            } else {
                FCRValues.leftWindowUI[0].leftText = "租借战车";
                FCRValues.rightWindowUI[5].rightText = "租借";
            }
            int m = 1;
            for (int i = showFristIndex; i < endI; i++) {
                FCRValues.leftWindowUI[m].leftText = langLib.getNameOfId(itemNames.get(i));
                if (page == 3) {
                    FCRValues.leftWindowUI[m].rightText = as.get(i) + "G";
                }
                m++;
            }
            FCRValues.rightWindowUI[3].rightText = "上一页";
            FCRValues.rightWindowUI[4].rightText = "下一页";
        } else if (page == 2) {
            FCRValues.leftWindowUI[0].leftText = "要归还那一俩";
            int m = 1;
            for (int i = showFristIndex; i < endI; i++) {
                FCRValues.leftWindowUI[m].leftText = langLib.getNameOfId(gameSave.getFcZucheTankList().get(i));
                m++;
            }
            FCRValues.rightWindowUI[5].rightText = "归还";
            FCRValues.rightWindowUI[3].rightText = "上一页";
            FCRValues.rightWindowUI[4].rightText = "下一页";
        }
        if (page == 0) {
            FCRValues.rightWindowUI[6].rightText = "退出";
        } else {
            FCRValues.rightWindowUI[6].rightText = "返回";
        }
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        //永远都是0到6
        if (entity.leftText.length() > 1) {
            if (page == 0) {
                page = i + 1;
                if (page == 2) {
                    showFristIndex = 0;
                    setEndIndex(Math.min(gameSave.getFcZucheTankList().size(), showFristIndex + 6));
                    resetUI();
                } else {
                    itemNames.clear();
                    as.clear();
                    itemNames.addAll(((ShopEvent) fcv.getPlayingEvent()).getItemid());
                    as.addAll(((ShopEvent) fcv.getPlayingEvent()).getItemprice());
                    //要减去已经卖出了的战车和租出的
                    gameSave.getTankSaveIos().forEach(new BiConsumer<String, FCTank>() {
                        @Override
                        public void accept(String s, FCTank tank) {
                            if (itemNames.contains(tank.getName())) {
                                int index = itemNames.indexOf(tank.getName());
                                as.remove(index);
                                itemNames.remove(tank.getName());
                            }
                        }
                    });

                    setEndIndex(Math.min(itemNames.size(), showFristIndex + 6));
                    resetUI();
                }
            } else if (page == 1 || page == 3) {
                if (itemNames.size() == 0) {
                    return;
                }
                FCTank tank = tankLib.getTankOfID(itemNames.get(showFristIndex + i - 1), gameSave.HOST_UID);
                if (tank != null) {
                    FCRValues.rightWindowUI[0].leftText = langLib.getNameOfId(tank.getName());
                    willBuyPerPrice = as.get(showFristIndex + i - 1);
                    willBuyItemName = tank.getName();
                }
            } else if (page == 2) {
                if (gameSave.getFcZucheTankList().size() == 0) {
                    return;
                }
                willBuyItemName = gameSave.getFcZucheTankList().get(showFristIndex + i - 1);
                FCRValues.rightWindowUI[0].leftText = "将要归还" + langLib.getNameOfId(willBuyItemName);
            }
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {

        switch (i) {
            case 3:
                if (page == 1 || page == 3) {
                    if (showFristIndex >= 6) {
                        showFristIndex -= 6;
                        setEndIndex(Math.min(itemNames.size(), showFristIndex + 6));
                        resetUI();
                    }
                } else if (page == 2) {
                    if (showFristIndex >= 6) {
                        showFristIndex -= 6;
                        setEndIndex(Math.min(gameSave.getFcZucheTankList().size(), showFristIndex + 6));
                        resetUI();
                    }
                }
                break;
            case 4:
                if (page == 1 || page == 3) {
                    if (endI < itemNames.size()) {
                        showFristIndex += 6;
                        setEndIndex(Math.min(itemNames.size(), showFristIndex + 6));
                        resetUI();
                    }
                } else if (page == 2) {
                    if (endI < gameSave.getFcZucheTankList().size()) {
                        showFristIndex += 6;
                        setEndIndex(Math.min(gameSave.getFcZucheTankList().size(), showFristIndex + 6));
                        resetUI();
                    }
                }
                break;
            case 5:
                FCTank tank = tankLib.getTankOfID(willBuyItemName, gameSave.HOST_UID);
                if (page == 1) {
                    //借
                    if (tank != null) {
                        String oldtankName = player1.getPlayertank1();
                        String oldTankHostId = player1.getPlayertankHostId();
                        tank.setType("Taxi");
                        if (!gameSave.getTankSaveIos().containsKey(tank.getName())) {
                            gameSave.getTankSaveIos().put(tank.getName(), tank);
                            fcv.getLocalPropulsions().add(tank);
                        }
                        gameSave.useTankOfPlayerId(player1, oldtankName, oldTankHostId);//先下车
                        gameSave.useTankOfPlayerId(player1, tank.getName(), tank.getHostId());//再上车
                        gameSave.useTankOfPlayerId(player1, tank.getName(), tank.getHostId());//再下车
                        gameSave.useTankOfPlayerId(player1, oldtankName, oldTankHostId);//上回旧车

                        int index = itemNames.indexOf(willBuyItemName);
                        itemNames.remove(willBuyItemName);
                        as.remove(index);
                        gameSave.getFcZucheTankList().add(willBuyItemName);
                        willBuyItemName = null;
                        showFristIndex = 0;
                        setEndIndex(Math.min(itemNames.size(), showFristIndex + 6));
                        resetUI();
                        FCRValues.rightWindowUI[0].leftText = "你已经租了" + gameSave.getFcZucheTankList().size() + "俩车";
                    }
                } else if (page == 2) {
                    if (tank != null) {
                        //判断谁正在乘坐的
                        if (willBuyItemName.equals(player1.getPlayertank1())) {
                            if (player1.getPlayertankHostId().equals(gameSave.HOST_UID)) {
                                gameSave.useTankOfPlayerId(player1, willBuyItemName, gameSave.HOST_UID);//下车
                            }
                        }
                        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
                            FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
                            if (willBuyItemName.equals(p.getPlayertank1())) {
                                if (p.getPlayertankHostId().equals(gameSave.HOST_UID)) {
                                    gameSave.useTankOfPlayerId(p, willBuyItemName,gameSave.HOST_UID);//下车
                                }
                            }
                        }
                        //2 然后移除
                        FCTank find = gameSave.findTankSaveIos(willBuyItemName);
                        if (find != null) {
                            gameSave.delTankItemClone(find.getChassisID());
                            gameSave.delTankItemClone(find.getEngineID());
                            gameSave.delTankItemClone(find.getRadarID());
                            gameSave.delTankItemClone(find.getGunID());
                            gameSave.delTankItemClone(find.getGun2ID());
                            gameSave.delTankItemClone(find.getCdeviceID());
                            gameSave.delTankItemClone(find.getSEID());
                            for (int t = 0; t < 10; t++) {
                                FCItem item = itemLib.getItemOfID(tank.getKnapsack()[t]);
                                if (item != null) {
                                    //放到保管处
                                    gameSave.getFcBankList().add(tank.getKnapsack()[t]);
                                }
                            }
                            fcv.getMessage().add("出租车背包物品将自动放入保管处!");
                            fcv.showMessage();
                            gameSave.getTankSaveIos().remove(find.getID());
                        }
                        tank.setVisable(0);
                        fcv.getLocalPropulsions().remove(tank);
                        FCMutilPlayPropulsionCache.loacalNoShareIt(tank);
                        gameSave.getFcZucheTankList().remove(willBuyItemName);
                        willBuyItemName = null;

                        showFristIndex = 0;
                        setEndIndex(Math.min(gameSave.getFcZucheTankList().size(), showFristIndex + 6));
                        resetUI();
                        FCRValues.rightWindowUI[0].leftText = "归还完成，感谢使用服务！";
                    }
                } else if (page == 3) {
                    //买
                    if (tank != null) {
                        if (player1.getMoney() >= willBuyPerPrice) {
                            tank.setType("Taxi");
                            if (!gameSave.getTankSaveIos().containsKey(tank.getName())) {
                                gameSave.getTankSaveIos().put(tank.getName(), tank);
                                fcv.getLocalPropulsions().add(tank);
                            }

                            String oldtankName = player1.getPlayertank1();
                            String oldTankHostId = player1.getPlayertankHostId();
                            gameSave.useTankOfPlayerId(player1, oldtankName, oldTankHostId);//先下车
                            gameSave.useTankOfPlayerId(player1, tank.getName(), tank.getHostId());//再上车
                            gameSave.useTankOfPlayerId(player1, tank.getName(), tank.getHostId());//再下车
                            gameSave.useTankOfPlayerId(player1, oldtankName, oldTankHostId);//上回旧车
                            tank.setType("owner");

                            int index = itemNames.indexOf(willBuyItemName);
                            itemNames.remove(willBuyItemName);
                            as.remove(index);
                            willBuyItemName = null;
                            player1.setMoney(player1.getMoney() - willBuyPerPrice);
                            showFristIndex = 0;
                            setEndIndex(Math.min(itemNames.size(), showFristIndex + 6));
                            resetUI();
                            FCRValues.rightWindowUI[0].leftText = "支付完成，感谢使用服务！";
                        } else {
                            FCRValues.rightWindowUI[0].leftText = "金额不够，你可以选择租借呢！";
                        }
                    }
                }
                break;
            case 6://取消
                if (page == 0) {
                    fcv.setOpenSayShop(false);
                    FCRMaster.fcv.setOpenSay(false);
                    FCRMaster.fcv.setSayNpcName(null);
                    FCRMaster.fcv.setSayNpcTextNum(0);
                } else {
                    page = 0;
                    resetUI();
                }
                break;
        }
    }

}
